package com.apes.fico.finance.service;

import com.apes.fico.finance.model.statements.CashFlowStatement;
import com.apes.fico.finance.respository.CashFlowStatementRepository;
import com.apes.fico.finance.respository.GLVoucherItemRepository;
import com.apes.fico.masterdata.gl.model.AccountingPeriodItem;
import com.apes.fico.masterdata.gl.respository.AccountingPeriodItemRepository;
import com.apes.fico.masterdata.gl.respository.CashFlowRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static com.apestech.framework.util.StringUtil.castToDouble;

/**
 * @ClassName CashFlowStatementService
 * @Description TODO
 * @Author PengZiFeng
 * @Date 2021/08/05 11:18
 * @Version 1.0
 **/
@Service("cashFlowStatementService")
public class CashFlowStatementService extends DomainService {

    @Autowired
    private AccountingPeriodItemRepository accountingPeriodItemRepository;
    @Autowired
    private CashFlowStatementRepository cashFlowStatementRepository;
    @Autowired
    private CashFlowRepository cashFlowRepository;
    @Autowired
    private GLVoucherItemRepository glVoucherItemRepository;

    /**
     * 查询现金流量表
     *
     * @param request
     * @return
     */
    public List<Map<String, String>> findAll(SimpleRequest request) {
        String company = request.get("company");
        int month = request.get("month");
        int stopMonth = request.get("tillMonth");
        int year = request.get("year");
        List<AccountingPeriodItem> accountingPeriodItem = accountingPeriodItemRepository.findByCompanyAndMonthAndStopMonthAndYear(company, month, stopMonth, year);
        if (accountingPeriodItem == null) throw new RuntimeException("无会计记录公司");



        List<Map<String, Double>> glVoucherItems = glVoucherItemRepository.findAllByCompanyAndMonthAndYear(company, month, stopMonth, year);
        List<Map<String, Double>> cash = glVoucherItemRepository.findCashByCompanyAndMonthAndYear(company, month, stopMonth, year);


        Map<String, Double> cashFlowMap = glVoucherItems.stream().
                collect(Collectors.groupingBy((Map map) -> map.get("CODE").toString(),
                        Collectors.summingDouble((Map map) -> Double.valueOf(map.get("MONEY").toString()))));

        Map<String, Double> cashFlow = cash.stream().
                collect(Collectors.groupingBy((Map map) -> map.get("CODE").toString(),
                        Collectors.summingDouble((Map map) -> Double.parseDouble(map.get("MONEY").toString()))));



        List<CashFlowStatement> cashFlowStateList = cashFlowStatementRepository.findAll();
        List<Map<String, String>> mapList = new ArrayList<>();
        Double money = 0.0;
        Double currencyCash = 0.0;
        for (CashFlowStatement cashFlowState : cashFlowStateList) {
            money += getSum(cashFlowState,cashFlowMap);
            Map<String, String> mapString = new HashMap<>();
            mapString.put("project", cashFlowState.getProjectName());
            mapString.put("sequence", cashFlowState.getSequence());

            if("c".equals(cashFlowState.getOrientation())){
                mapString.put("currentAmountYear", String.format("%.2f", money));
                money = 0.0;
            }else {
                mapString.put("currentAmountYear", String.format("%.2f", getSum(cashFlowState, cashFlowMap)) );
            }

            if("+".equals(cashFlowState.getOrientation()) && cashFlowState.getProjectFormula() != null){
                if(cashFlow.get("6603090000") != null) {
                    currencyCash = cashFlow.get("6603090000");
                    mapString.put("currentAmountYear", String.format("%.2f", currencyCash));
                }else{
                    mapString.put("currentAmountYear", "");
                }
            }
            mapList.add(mapString);
        }
        mapList = callMapList(mapList,currencyCash);
        return mapList;
    }
    private List<Map<String, String>> callMapList(List<Map<String, String>> mapList,Double currencyCash) {
        Double InA = Double.valueOf(mapList.get(4).get("currentAmountYear"));
        Double OutA = Double.valueOf(mapList.get(9).get("currentAmountYear"));
        Double InB = Double.valueOf(mapList.get(16).get("currentAmountYear"));
        Double OutB = Double.valueOf(mapList.get(20).get("currentAmountYear"));
        Double InC = Double.valueOf(mapList.get(26).get("currentAmountYear"));
        Double OutC = Double.valueOf(mapList.get(30).get("currentAmountYear"));
        mapList.get(10).put("currentAmountYear",String.format("%.2f", InA-OutA));
        mapList.get(21).put("currentAmountYear",String.format("%.2f", InB-OutB));
        mapList.get(31).put("currentAmountYear",String.format("%.2f", InC-OutC)+"");
        mapList.get(33).put("currentAmountYear",String.format("%.2f", (InA-OutA)+(InB-OutB)+(InC-OutC)+currencyCash));
        return mapList;
    }

    public Double getSum(CashFlowStatement cashFlowState,Map<String,Double> cashFlowMap){
        Double money = 0.0;
        if("+".equals(cashFlowState.getOrientation()) && cashFlowMap.get(cashFlowState.getProjectNum()) != null){
            money = cashFlowMap.get(cashFlowState.getProjectNum());
        }
        if("-".equals(cashFlowState.getOrientation()) && cashFlowMap.get(cashFlowState.getProjectNum()) != null){
            money = ((cashFlowMap.get(cashFlowState.getProjectNum())) * -1);
        }

        return money;
    }



}

